package com.gsafety.library.Widgets;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.gsafety.library.Exts.Ext;
import com.gsafety.library.R;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;

/**
 * 自定义的头部刷新控件
 */
public class RefreshHeaderView extends LinearLayout implements RefreshHeader
{
    private ImageView drag_img;
    private ImageView loading_img;
    private TextView tip;

    private ValueAnimator anim_loading;
    private ViewPropertyAnimator anim_drag;

    public RefreshHeaderView(Context context)
    {
        this(context, null);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.layout_default_header_view, this, true);
        drag_img = view.findViewById(R.id.drag_img);
        loading_img = view.findViewById(R.id.loading_img);
        tip = view.findViewById(R.id.tip);

        anim_drag = drag_img.animate();
        anim_drag.setDuration(200);

        anim_loading = ObjectAnimator.ofFloat(loading_img, "rotation", 0f, 360f);
        anim_loading.setDuration(1000);
        anim_loading.setInterpolator(new LinearInterpolator());
        anim_loading.setRepeatCount(-1);

        drag_img.setRotationX(0.5f);
        drag_img.setRotationY(0.5f);
        loading_img.setRotationX(0.5f);
        loading_img.setRotationY(0.5f);
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle()
    {
        return SpinnerStyle.Translate;
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success)
    {
        return 500;
    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight)
    {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax)
    {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight)
    {

    }

    @NonNull
    @Override
    public View getView()
    {
        return this;
    }

    @Override
    public void setPrimaryColors(int... colors)
    {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight)
    {

    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState)
    {
        if (newState == RefreshState.PullDownToRefresh)// 下拉开始刷新
        {
            Ext.gone(loading_img);
            Ext.show(drag_img);
            tip.setText("下拉可以刷新");
            anim_drag.rotation(0f);
            anim_drag.start();

        } else if (newState == RefreshState.ReleaseToRefresh)// 释放立即刷新
        {
            tip.setText("松开立即刷新");
            anim_drag.rotation(180f);
            anim_drag.start();
        } else if (newState == RefreshState.Refreshing || newState == RefreshState.RefreshReleased)
        {
            tip.setText("正在刷新");
            Ext.gone(drag_img);
            Ext.show(loading_img);
            if (!anim_loading.isRunning())
            {
                anim_loading.start();
            }
        } else if (newState == RefreshState.RefreshFinish)// 结束刷新
        {
            tip.setText("刷新结束");
            anim_loading.cancel();
            Ext.gone(loading_img);
            Ext.gone(drag_img);
        } else if (newState == RefreshState.None) // 结束
        {
            Ext.gone(loading_img);
            Ext.show(drag_img);
        }
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight)
    {

    }

    @Override
    public boolean isSupportHorizontalDrag()
    {
        return false;
    }
}
